package com.springboot.shiyan2.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

@EnableConfigurationProperties(testProperties.class)
public class autoConfig {

    @Bean
    @ConditionalOnProperty(prefix = "lhc.auto", name = "enable", havingValue = "true")
    CommandLineRunner createCommandLineRunner(Environment env){
        return new myCommandLineRunner(env);
    }

    @Bean
    public ThreadPoolTaskExecutor taskExector() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor ();
        Executor executorService = Executors.newCachedThreadPool ();//获取到服务器的cpu内核
        executor.setCorePoolSize (5);//核心池大小
        executor.setMaxPoolSize (100);//最大线程数
        executor.setQueueCapacity (1000);//队列程度
        executor.setKeepAliveSeconds (1000);//线程空闲时间
        executor.setThreadNamePrefix ("task-asyn");//线程前缀名称
        executor.setRejectedExecutionHandler (new ThreadPoolExecutor.AbortPolicy ());//配置拒绝策略
        return executor;
    }

    static class NoticeEvent extends ApplicationEvent {
        private  final Logger logger = LoggerFactory.getLogger(NoticeEvent.class);
        private final String message;

        public NoticeEvent(String message) {
            super(message);
            this.message = message;
            logger.info("添加事件成功！ message：" + message);
        }
        public String getMessage() {
            return message;
        }
    }

    @Component
    static class NoticeListener implements ApplicationListener<NoticeEvent> {
        private static final Logger logger = LoggerFactory.getLogger(NoticeListener.class);
        @Override
        public void onApplicationEvent(NoticeEvent noticeEvent) {
            System.out.println ("test listener ");
            logger.info("事件监听器获取到 NoticeEvent ， 睡眠当前进程3秒...");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            logger.info("noticeEvent 的 message 属性为 ：{}", noticeEvent.getMessage());
        }
    }

    @Bean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME)
    ApplicationEventMulticaster createCustomApplicationEventMulticaster(@Qualifier("taskExector") ThreadPoolTaskExecutor threadPoolTaskExecutor) {
        SimpleApplicationEventMulticaster simpleApplicationEventMulticaster = new SimpleApplicationEventMulticaster();
        simpleApplicationEventMulticaster.setTaskExecutor(threadPoolTaskExecutor);
        return simpleApplicationEventMulticaster;
    }



}
